home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 May / CMCD0504.ISO / Software / Freeware / Programare / gdiplusdelphi / demos / Using Text and Fonts / Formatting Text / GDITEST54.dpr
Encoding:
Text File  |  2003-10-15  |  3.0 KB  |  115 lines

  1. program GDITEST54;
  2.  
  3. uses
  4.   Windows,
  5.   Messages,
  6.   SysUtils,
  7.   GDIPAPI,
  8.   GDIPOBJ;
  9.  
  10. Procedure OnPaint(DC: HDC);
  11. var
  12.   graphics : TGPGraphics;
  13.   str: String;
  14.   fontFamily: TGPFontFamily;
  15.   font: TGPFont;
  16.   rectF: TGPRectF;
  17.   stringFormat: TGPStringFormat;
  18.   solidBrush: TGPSolidBrush;
  19.   pen: TGPPen;
  20. begin
  21.   graphics := TGPGraphics.Create(DC);
  22.   str := 'Use StringFormat and RectF objects to center text in a rectangle.';
  23.  
  24.   fontFamily:= TGPFontFamily.Create('Arial');
  25.   font:= TGPFont.Create(fontFamily, 12, FontStyleBold, UnitPoint);
  26.   rectF:= MakeRect(30.0, 10.0, 120.0, 140.0);
  27.   stringFormat:= TGPStringFormat.Create;
  28.   solidBrush:= TGPSolidBrush.Create(MakeColor(255, 0, 0, 255));
  29.  
  30.   // Center-justify each line of text.
  31.   stringFormat.SetAlignment(StringAlignmentCenter);
  32.  
  33.   // Center the block of text (top to bottom) in the rectangle.
  34.   stringFormat.SetLineAlignment(StringAlignmentCenter);
  35.  
  36.   graphics.DrawString(str, -1, font, rectF, stringFormat, solidBrush);
  37.  
  38.   pen:= TGPPen.Create(MakeColor(255, 0, 0, 0));
  39.   graphics.DrawRectangle(pen, rectF);
  40.  
  41.   fontFamily.Free;
  42.   font.Free;
  43.   stringFormat.Free;
  44.   solidBrush.Free;
  45.   pen.Free;
  46.   graphics.Free;
  47. end;
  48.  
  49.  
  50. function WndProc(Wnd : HWND; message : UINT; wParam : Integer; lParam: Integer) : Integer; stdcall;
  51. var
  52.   Handle: HDC;
  53.   ps: PAINTSTRUCT;
  54. begin
  55.   case message of
  56.     WM_PAINT:
  57.       begin
  58.         Handle := BeginPaint(Wnd, ps);
  59.         OnPaint(Handle);
  60.         EndPaint(Wnd, ps);
  61.         result := 0;
  62.       end;
  63.  
  64.     WM_DESTROY:
  65.       begin
  66.         PostQuitMessage(0);
  67.         result := 0;
  68.       end;
  69.  
  70.    else
  71.       result := DefWindowProc(Wnd, message, wParam, lParam);
  72.    end;
  73. end;
  74.  
  75. var
  76.   hWnd     : THandle;
  77.   Msg      : TMsg;
  78.   wndClass : TWndClass;
  79. begin
  80.    wndClass.style          := CS_HREDRAW or CS_VREDRAW;
  81.    wndClass.lpfnWndProc    := @WndProc;
  82.    wndClass.cbClsExtra     := 0;
  83.    wndClass.cbWndExtra     := 0;
  84.    wndClass.hInstance      := hInstance;
  85.    wndClass.hIcon          := LoadIcon(0, IDI_APPLICATION);
  86.    wndClass.hCursor        := LoadCursor(0, IDC_ARROW);
  87.    wndClass.hbrBackground  := HBRUSH(GetStockObject(WHITE_BRUSH));
  88.    wndClass.lpszMenuName   := nil;
  89.    wndClass.lpszClassName  := 'GettingStarted';
  90.  
  91.    RegisterClass(wndClass);
  92.  
  93.    hWnd := CreateWindow(
  94.       'GettingStarted',       // window class name
  95.       'Formatting Text',       // window caption
  96.       WS_OVERLAPPEDWINDOW,    // window style
  97.       Integer(CW_USEDEFAULT), // initial x position
  98.       Integer(CW_USEDEFAULT), // initial y position
  99.       Integer(CW_USEDEFAULT), // initial x size
  100.       Integer(CW_USEDEFAULT), // initial y size
  101.       0,                      // parent window handle
  102.       0,                      // window menu handle
  103.       hInstance,              // program instance handle
  104.       nil);                   // creation parameters
  105.  
  106.    ShowWindow(hWnd, SW_SHOW);
  107.    UpdateWindow(hWnd);
  108.  
  109.    while(GetMessage(msg, 0, 0, 0)) do
  110.    begin
  111.       TranslateMessage(msg);
  112.       DispatchMessage(msg);
  113.    end;
  114. end.
  115.